Ext.require([
    'Ext.grid.*',
    'Ext.data.*',
    'Ext.panel.*',
    'Ext.tip.QuickTipManager',
    'Ext.window.MessageBox'
]);

var url;

Ext.onReady(function() {
	
	Ext.tip.QuickTipManager.init();
	
	url = APP_PATH + "/getSystemUserJsonData.action";
	
	Ext.define('GridModel', {
        extend: 'Ext.data.Model',
        fields: ['id', 'userName', 'chineseName', 'roleID', 'unsuccessfulAttemptNum', 'emailAddr', 'lastPasswordUpdateDate', 'lastLoginDate', 'lastLoginIP', 
                 'lastLoginIPAddr', 'status', 'loginStatus', 'remarks']
    });
	
	var store = Ext.create('Ext.data.JsonStore', {
        model: 'GridModel',
        proxy: {
            type: 'ajax',
            url: url,
            reader: {
                type: 'json',
                root: 'root',
                totalProperty: 'totalCount'
            }
        }
    });
	
	var cm = [{text: 'id', flex: 30, dataIndex: 'id', hidden: true},
	          {text: '用户名', flex: 25, dataIndex: 'userName'},
	          {text: '真实姓名', flex: 35, dataIndex: 'chineseName'},
	          {text: '用户组', flex: 35, dataIndex: 'roleID'},
	          {text: '登录失败次数', flex: 35, dataIndex: 'unsuccessfulAttemptNum'},
	          {text: 'Email', flex: 35, dataIndex: 'emailAddr', hidden: true},
	          {text: '最后密码修改时间', flex: 35, dataIndex: 'lastPasswordUpdateDate', hidden: true },
	          {text: '最后登录时间', flex: 35, dataIndex: 'lastLoginDate', hidden: true},
	          {text: '最后登录IP', flex: 35, dataIndex: 'lastLoginIP'},
	          {text: '最后登录地址', flex: 60, dataIndex: 'lastLoginIPAddr'},
	          {text: '用户状态', flex: 35, dataIndex: 'status'},
	          {text: '用户登录状态', flex: 35, dataIndex: 'loginStatus'},
	          {text: '备注', flex: 35, dataIndex: 'remarks', hidden: true},
	          {text: '操作', flex: 50, dataIndex: 'id', renderer: renderOption}
	         ];
	
	var tbar = [{text: '添加', iconCls: 'add', handler: uf_Add},
				{text: '重新加载', iconCls: 'refresh', handler: uf_Query}
				];
	
	var listView = Ext.create('Ext.grid.Panel', {
		selType: 'checkboxmodel',
        height: 550,
        frame: true,
        collapsible: true,
        title: '<b>用户管理</b>',
        iconCls: 'icon-grid',
        renderTo: Ext.get('grid'),
        store: store,
        tbar: tbar,
        multiSelect: true,
        collapsible: false,
        viewConfig: {
            emptyText: '<center>没有查询到相关记录!</center>'
        },
        columns: cm,
        bbar: new Ext.PagingToolbar({
            pageSize: 20,
            store: store,
            displayInfo: true,
            displayMsg: '显示第 <b>{0}</b> 条到 <b>{1}</b> 条记录, 一共 <b>{2}</b> 条',
            emptyMsg: '没有记录'
           })
    });
	
	store.load({ params: { start: 0, limit: 20} }); 
	
	function uf_Add() {
		
		submitForm("systemUserNew.action");
	}
	
	function uf_Query() {
		
		store.load();
	}
	
	function renderOption(val, cellmeta, record, rowIndex, columnIndex, store) {
		
		var str = createA('编辑', 'uf_Edit', val);
		str += createA('删除', 'uf_Del', val);
		
		return str;
	}
});

function uf_Query() {
	
	store.load();
}

function uf_Edit(id) {
	
	submitForm("systemUserEdit.action?systemUserId=" + id);
}

function uf_Del(id) {
	
	if(!confirm('您确认删除该记录吗?')) return false;
	
	Ext.Ajax.request({
    	waitMsg:'正在加载数据，请稍侯……', 
	    url: 'systemUserAjaxDelete.action',
	    method:'POST',
	 	params: {systemUserId: id},
		success:function(result, request){
			if(result.responseText == "") {
				uf_Query();
			} else {
				alert(result.responseText);
			}
		} 
	});
}

function uf_Return(id) {
	
	submitForm("systemUserList.action");
}